package cn.bdqn.smbms.service;

import cn.bdqn.smbms.dao.UserDao;
import cn.bdqn.smbms.entity.User;
import cn.bdqn.smbms.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao ;

    @Override
    public User findByCodeAndPwd(String userCode, String userPassword) {
        User user = userDao.findByCode(userCode);
        if(user!=null&&user.getUserPassword().equals(userPassword)){
            return user;
        }else{
            return null;
        }
    }

    @Override
    public PageBean<User> findByPage(String queryName,Integer queryUserRole,int pageNo, int pageSize) {
        PageBean<User> pageBean = new PageBean<>();
        pageBean.setPageSize(pageSize);
        pageBean.setTotalCount(userDao.getTotalCount(queryName,queryUserRole));
        pageBean.setPageNo(pageNo);
        int from = (pageBean.getPageNo()-1)*pageSize;
        List<User> pageList = userDao.findByPage(queryName,queryUserRole,from,pageSize);
        pageBean.setPageList(pageList);
        return pageBean;
    }

    @Override
    public boolean checkUserCode(String userCode) {
        User user = userDao.findByCode(userCode);
        if(user!=null) {
            return true;
        }
        return false;
    }

    @Override
    public int addUser(User user) {
        return userDao.addUser(user);
    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateUser(user);
    }

    @Override
    public int delUser(int id) {
        return userDao.delUser(id);
    }

}
